'''
Created on Feb 21, 2013

@author: Justin
'''

import pygame,sys
from pygame.locals import *

from player import Player
from unit import Knight
from enemies import *
from images import *

pygame.init()


class Game (object):

    def __init__ (self, argv):
    
        self.screen = pygame.display.set_mode((900,700))
        self.rect = self.screen.get_rect()
        self.clock = pygame.time.Clock()
        self.time_passed = 0
        self.running = True
        self.container_list = [Background.container, # Background must be first
                               Enemy.container, Knight.container ]
        # Fonts
        self.main_font = pygame.font.SysFont(pygame.font.get_default_font(), 20)
        # Game Data
        self.background = Background(MOSSY_ROCK)
        self.player = Player()
        self.wave = 1
        self.error_message_timer = 0
        self.error_message = None

    def spawnEnemies (self):
        unit_count = self.wave * 1
        for i in range(unit_count):
            SwampEnemy(self.rect, self.player)

    def updateAllContainers (self):
        
        for container in self.container_list:
            container.update(self.time_passed)
            container.draw(self.screen)

    def displayStats (self):
        fr = self.main_font.render
        text = "Wall Strength: %s" %(self.player.health)
        surface = fr(text, True, (255,255,255))
        self.screen.blit(surface, (10, 10))
        text = "Money Remaining: %s" % (self.player.money)
        surface = fr(text, True, (255,255,255))
        self.screen.blit(surface, (10, surface.get_height() * 2))

    def nextWave(self):
        self.wave += 1
        self.spawnEnemies()

    def setErrorMessage(self, message="Invalid order!"):
        self.error_message = self.main_font.render(message, True, (255, 100, 100))
        self.error_message_timer = 120 # display error message for 120 frames

    def run (self):
    
        while self.running:
            self.time_passed = self.clock.tick(60)/1000.0
            for event in pygame.event.get():
                if event.type == QUIT: self.running = False
                elif event.type == KEYUP:
                    if event.key == K_ESCAPE: self.running = False
                elif event.type == MOUSEBUTTONUP:
                    pos = pygame.mouse.get_pos()
                    if self.player.money > 0:
                        self.player.money -= 1
                        Knight(Knight.container).setPosition(pos)
                    else:
                        self.setErrorMessage("No money remaining!")

            if not Enemy.container: # If there are no enemies, move to next wave
                self.nextWave()
            self.updateAllContainers()
            pygame.sprite.groupcollide(Enemy.container, Knight.container, True, True)
            self.displayStats()
            # display error message
            if self.error_message_timer > 0:
                self.screen.blit(self.error_message, (10, self.screen.get_height() - (self.error_message.get_height() * 2)))
                self.error_message_timer -= 1
            pygame.display.flip()

if __name__ == "__main__":
    game = Game(sys.argv)
    game.run()
    # Attempt to close after game stops running
    pygame.quit()
    try: sys.exit()
    except: pass
